package be.jaisy.project.adapter.internal;

import java.util.Hashtable;

import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;

import be.jaisy.osgi.AbstractActivator;
import be.jaisy.osgi.transaction.ITransactionManager;
import be.jaisy.project.adapter.IProjectDao;
import be.jaisy.project.adapter.factory.DaoFactory;
import be.jaisy.project.adapter.factory.TransactionFactory;

/**
 * @author Stany Blanvalet
 */
public class Activator extends AbstractActivator {
	
	private ServiceRegistration transactionManager;
	private ServiceRegistration projectDao;
	
	@Override
	public void start(BundleContext context) throws Exception {		
		
		Hashtable<String, String> properties = new Hashtable<String, String>();

		properties.put(Constants.SERVICE_VENDOR, (String)context.getBundle().getHeaders().get(Constants.BUNDLE_VENDOR));
		properties.put(Constants.SERVICE_DESCRIPTION, "Transaction manager for customer of be.jaisy.project.adapter exposed services");

		transactionManager = context.registerService(ITransactionManager.class.getName(), TransactionFactory.instance(TransactionFactory.HIBERNATE, context.getBundle()), properties);

		properties.put(Constants.SERVICE_DESCRIPTION, "IProjectDao exposed services");

		projectDao = context.registerService(IProjectDao.class.getName(), DaoFactory.instance(DaoFactory.HIBERNATE, context.getBundle()), properties);
		super.start(context);		
	}

	@Override
	public void stop(BundleContext context) throws Exception {
		
		if(transactionManager!=null){
			transactionManager.unregister();
			transactionManager = null;
		}
		if(projectDao!=null){
			projectDao.unregister();
			projectDao = null;
		}			
		super.stop(context);
	}
}
